package talk.dapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.talk.debby.talk.R;

import java.util.List;

import talk.Bean.Chapter;
import talk.Bean.Course;
import talk.listener.OnItemClickListener;

/**
 * Created by debby on 2018/5/25.
 * 描述:
 */

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
    private Context context;
    private List<Course> courseList;
    private OnItemClickListener clickListener;


    public ItemAdapter(Context context, List<Course> courseList, OnItemClickListener clickListener) {
        this.context = context;
        this.courseList = courseList;
        this.clickListener = clickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(context)
                .inflate(R.layout.adapter_catlog, parent, false);
        return new ViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final Course course = courseList.get(position);
        holder.tv_course.setText(course.getCourse());
        setTextDrawable(holder.tv_course, false);

        if (course.getChapterList() == null || course.getChapterList().size() < 1) {
            setTextDrawable(holder.tv_course);//没有下拉菜单置灰，没有图片
            holder.tv_course.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "没有内容", Toast.LENGTH_SHORT).show();
                }
            });
            return;
        }
        holder.tv_course.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.ll_chapters.getVisibility() == View.VISIBLE) {
                    holder.ll_chapters.setVisibility(View.GONE);
                    setTextDrawable(holder.tv_course, false);
                } else {
                    holder.ll_chapters.setVisibility(View.VISIBLE);
                    setTextDrawable(holder.tv_course, true);
                }
            }
        });
        //
        addTitle(holder.ll_chapters, course);
    }


    @Override
    public int getItemCount() {
        return courseList == null ? 0 : courseList.size();
    }

    private void addTitle(LinearLayout ll_chapters, Course course) {
        for (final Chapter chapter : course.getChapterList()) {
            View view = LayoutInflater.from(context).inflate(
                    R.layout.adapter_sub_catlog, null);
            TextView tv_chapter = (TextView) view.findViewById(R.id.tv_chapter);
            tv_chapter.setText(chapter.getChapter());
            tv_chapter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onSelect(chapter.getUrlList());
                }
            });
            ll_chapters.addView(view);
        }
    }

    //设置图片
    private void setTextDrawable(TextView tv_course, Boolean open) {
        if (open) {
            Drawable drawable = context.getResources().getDrawable(R.mipmap.aaa_open);
            drawable.setBounds(0, 0, 60, 60);
            tv_course.setCompoundDrawables(null, null, drawable, null);
            tv_course.setTextColor(Color.rgb(53, 173, 228));
        } else {
            Drawable drawable = context.getResources().getDrawable(R.mipmap.aaa_close);
            drawable.setBounds(0, 0, 60, 60);
            tv_course.setCompoundDrawables(null, null, drawable, null);
            tv_course.setTextColor(Color.rgb(68, 68, 68));
        }
    }

    //设置图片
    private void setTextDrawable(TextView tv_course) {
        tv_course.setCompoundDrawables(null, null, null, null);
        tv_course.setTextColor(Color.rgb(170, 170, 170));
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_course;
        private LinearLayout ll_chapters;

        public ViewHolder(View rootView) {
            super(rootView);
            tv_course = (TextView) rootView.findViewById(R.id.tv_course);
            ll_chapters = (LinearLayout) rootView.findViewById(R.id.ll_chapters);
        }
    }


}
